#include "stdafx.h"
#include "NdaXrecord.h"
#include "nda.h"
using namespace NS_NDA;


void CNdaXRecord::SetParam(SingleHisDfData *pRecord, int complexRD)
{
	m_pRecord = pRecord;
	m_nOneRdSize = complexRD;
	if (complexRD > sizeof(SingleHisDfData))
	{
		Clear();
		BYTE *buf = (BYTE *)pRecord;
		int nPos = sizeof(SingleHisDfData);
		memcpy(&m_btHaveBms, buf+nPos, sizeof(m_btHaveBms));
		nPos += sizeof(m_btHaveBms);
		if (m_btHaveBms == 1)
		{
			memcpy(&m_bmsInfo, buf+nPos, sizeof(short)*4);
			nPos  += sizeof(short)*4;
			if (m_bmsInfo.nStDataCount > 0)
			{
				m_bmsInfo.pStData = new DInfo[m_bmsInfo.nStDataCount];
				memcpy(m_bmsInfo.pStData, buf+nPos, m_bmsInfo.nStDataCount*sizeof(DInfo));
				nPos += m_bmsInfo.nStDataCount*sizeof(DInfo);
			}

			if (m_bmsInfo.nVolCount > 0)
			{
				m_bmsInfo.pVolData = new SingleInfo[m_bmsInfo.nVolCount];
				memcpy(m_bmsInfo.pVolData, buf+nPos, m_bmsInfo.nVolCount*sizeof(SingleInfo));
				nPos += m_bmsInfo.nVolCount*sizeof(SingleInfo);
			}

			if (m_bmsInfo.nTempCount > 0)
			{
				m_bmsInfo.pTempData = new SingleInfo[m_bmsInfo.nTempCount];
				memcpy(m_bmsInfo.pTempData, buf+nPos, m_bmsInfo.nTempCount*sizeof(SingleInfo));
				nPos += m_bmsInfo.nTempCount*sizeof(SingleInfo);
			}

			if (m_bmsInfo.nSelfCount > 0)
			{
				m_bmsInfo.pSelfData = new DInfo[m_bmsInfo.nSelfCount];
				memcpy(m_bmsInfo.pSelfData, buf+nPos, m_bmsInfo.nSelfCount*sizeof(DInfo));
				nPos += m_bmsInfo.nSelfCount*sizeof(DInfo);
			}
		} 

		if (complexRD <= nPos)
			return;

		memcpy(&m_btHaveAux, buf+nPos, sizeof(m_btHaveAux));
		nPos += sizeof(m_btHaveAux);
		if (m_btHaveAux)
		{
			memcpy(&m_auxInfo, buf+nPos, sizeof(short)*3);
			nPos  += sizeof(short)*3;
			if (m_auxInfo.nStDataCount > 0)
			{
				m_auxInfo.pStData = new DInfo[m_auxInfo.nStDataCount];
				memcpy(m_auxInfo.pStData, buf+nPos, m_auxInfo.nStDataCount*sizeof(DInfo));
				nPos += m_auxInfo.nStDataCount*sizeof(DInfo);
			}

			if (m_auxInfo.nVolCount > 0)
			{
				m_auxInfo.pVolData = new float[m_auxInfo.nVolCount];
				memcpy(m_auxInfo.pVolData, buf+nPos, m_auxInfo.nVolCount*sizeof(float));
				nPos += m_auxInfo.nVolCount*sizeof(float);
			}

			if (m_auxInfo.nTempCount > 0)
			{
				m_auxInfo.pTempData = new float[m_auxInfo.nTempCount];
				memcpy(m_auxInfo.pTempData, buf+nPos, m_auxInfo.nTempCount*sizeof(float));
				nPos += m_auxInfo.nTempCount*sizeof(float);
			}
		}
	} 
	
}
DWORD CNdaXRecord::GetRecordID()
{
	return m_pRecord->dwTestSn;
}
double CNdaXRecord::GetCur ()
{
	return m_pRecord->nCurr * SCALE_IU ;
}
double CNdaXRecord::GetVol ()
{
	return m_pRecord->nVolt * SCALE_IU;
}
double CNdaXRecord::GetCap ()
{
	return m_pRecord->nCap * SCALE_CN;
}
double CNdaXRecord::GetEng ()
{
	return m_pRecord->nEngSum * SCALE_CN;
}
double CNdaXRecord::GetIR ()
{
	return m_pRecord->nInterRes * SCALE_IR;
}
double CNdaXRecord::GetTemp ()
{
	return m_pRecord->nTempature * SCALE_TR;
}
UINT64 CNdaXRecord::GetTime ()
{
	return m_pRecord->recordTime > 0 ? m_pRecord->recordTime : 0;
}
UINT64 CNdaXRecord::GetLocalTime ()
{
	return m_pRecord->dTestTime;
}

void CNdaXRecord::CopyTo (CNdaXRecord& newRecord)
{
	memcpy( &newRecord.m_copyedRecord,m_pRecord,m_nOneRdSize );
	newRecord.m_pRecord = &newRecord.m_copyedRecord;
	newRecord.m_nOneRdSize = m_nOneRdSize;
	newRecord.m_btHaveBms = m_btHaveBms;
	int nPos = sizeof(SingleHisDfData) + sizeof(BYTE);
	if (m_nOneRdSize != sizeof(SingleHisDfData) && m_btHaveBms == 1)
	{
		BYTE *buf = (BYTE *)m_pRecord;
		
		memcpy(&newRecord.m_bmsInfo, buf+nPos, sizeof(short)*4);
		nPos  += sizeof(short)*4;
		if (m_bmsInfo.nStDataCount > 0)
		{
			newRecord.m_bmsInfo.pStData = new DInfo[m_bmsInfo.nStDataCount];
			memcpy(newRecord.m_bmsInfo.pStData, buf+nPos, m_bmsInfo.nStDataCount*sizeof(DInfo));
			nPos += m_bmsInfo.nStDataCount*sizeof(DInfo);
		}

		if (m_bmsInfo.nVolCount > 0)
		{
			newRecord.m_bmsInfo.pVolData = new SingleInfo[m_bmsInfo.nVolCount];
			memcpy(newRecord.m_bmsInfo.pVolData, buf+nPos, m_bmsInfo.nVolCount*sizeof(SingleInfo));
			nPos += m_bmsInfo.nVolCount*sizeof(SingleInfo);
		}

		if (m_bmsInfo.nTempCount > 0)
		{
			newRecord.m_bmsInfo.pTempData = new SingleInfo[m_bmsInfo.nTempCount];
			memcpy(newRecord.m_bmsInfo.pTempData, buf+nPos, m_bmsInfo.nTempCount*sizeof(SingleInfo));
			nPos += m_bmsInfo.nTempCount*sizeof(SingleInfo);
		}

		if (m_bmsInfo.nSelfCount > 0)
		{
			newRecord.m_bmsInfo.pSelfData = new DInfo[m_bmsInfo.nSelfCount];
			memcpy(newRecord.m_bmsInfo.pSelfData, buf+nPos, m_bmsInfo.nSelfCount*sizeof(DInfo));
			nPos += m_bmsInfo.nSelfCount*sizeof(DInfo);

		}
	} 
	if (m_btHaveAux == 1)
	{
		BYTE *buf = (BYTE *)m_pRecord;
		nPos +=sizeof(BYTE);
		memcpy(&newRecord.m_auxInfo, buf+nPos, sizeof(short)*3);
		nPos  += sizeof(short)*3;
		if (m_auxInfo.nStDataCount > 0)
		{
			newRecord.m_auxInfo.pStData = new DInfo[m_auxInfo.nStDataCount];
			memcpy(newRecord.m_auxInfo.pStData, buf+nPos, m_auxInfo.nStDataCount*sizeof(DInfo));
			nPos += m_auxInfo.nStDataCount*sizeof(DInfo);
		}

		if (m_auxInfo.nVolCount > 0)
		{
			newRecord.m_auxInfo.pVolData = new float[m_auxInfo.nVolCount];
			memcpy(newRecord.m_auxInfo.pVolData, buf+nPos, m_auxInfo.nVolCount*sizeof(float));
			nPos += m_auxInfo.nVolCount*sizeof(float);
		}

		if (m_auxInfo.nTempCount > 0)
		{
			newRecord.m_auxInfo.pTempData = new float[m_auxInfo.nTempCount];
			memcpy(newRecord.m_auxInfo.pTempData, buf+nPos, m_auxInfo.nTempCount*sizeof(float));
			nPos += m_auxInfo.nTempCount*sizeof(float);
		}
	} 
}

BYTE CNdaXRecord::GetStepType( void )
{
	return m_pRecord->btStepType;
}

bool CNdaXRecord::IsInit()
{
	return NULL != m_pRecord;
}

BOOL CNdaXRecord::SelfCheck( DWORD& dwPreRecordId )
{
	if (GetRecordID() <= 0 || GetStepType() == 0 ||GetStepType() >= stStepCount)
	{
		return FALSE;
	}

	return TRUE;
}

DWORD CNdaXRecord::GetCycleID( void )
{
	return m_pRecord->dwCycle;
}

DWORD CNdaXRecord::GetStepID( void )
{
	return m_pRecord->stepSeq;
}

DWORD CNdaXRecord::GetStepNo()
{
	return m_pRecord->stepID;
}

UINT64 CNdaXRecord::GetTimeOffStep()
{
	return m_pRecord->stepTime;
}

void CNdaXRecord::Clear()
{
	if (m_btHaveBms)
	{
		if (m_bmsInfo.nStDataCount > 0 && m_bmsInfo.pStData)
			delete [] m_bmsInfo.pStData;
		if (m_bmsInfo.nVolCount > 0 && m_bmsInfo.pVolData)
			delete [] m_bmsInfo.pVolData;
		if (m_bmsInfo.nTempCount > 0 && m_bmsInfo.pTempData)
			delete [] m_bmsInfo.pTempData;
		if (m_bmsInfo.nSelfCount > 0 && m_bmsInfo.pSelfData)
			delete [] m_bmsInfo.pSelfData;
	} 
}

BmsPktInfo& CNdaXRecord::GetBms()
{
	return m_bmsInfo;
}

BYTE CNdaXRecord::GetBmsFlag()
{
	return m_btHaveBms;
}

AuxChlData& CNdaXRecord::GetAux()
{
	return m_auxInfo;
}

BYTE CNdaXRecord::GetAuxFlag()
{
	return m_btHaveAux;
}

